import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter_rider/main/widgets/base/detail_flexStart_layout.dart';
import 'package:flutter_rider/module/mine/models/shunting_model.dart';
import 'package:flutter_rider/main/widgets/ext/u_layout.dart';
import 'package:flutter_rider/main/widgets/ext/u_text.dart';
import 'package:flutter_rider/main/widgets/ext/u_title_layout.dart';
import 'package:flutter_rider/main/widgets/state/state_layout.dart';
import 'package:flutter_rider/main/widgets/ui_constant.dart';
import 'package:flutter_rider/utils/screen_fit.dart';

class ShuntimeDetailPage extends StatefulWidget {
  final int type;
  const ShuntimeDetailPage(this.type, {Key? key}) : super(key: key);

  @override
  State<ShuntimeDetailPage> createState() => _ShuntimeDetailPageState();
}

class _ShuntimeDetailPageState extends State<ShuntimeDetailPage> {
  final StateLayoutController _stateLayoutController = StateLayoutController();
  //dynamic _data;
  ShuntingModel? _data;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: UIColor.color_main,
      appBar: UTitleLayout.getBackTitle(context, "调车详情"),
      body: StateLayout(
        url: "/user/car/dispatch_info?id=${widget.type}",
        controller: _stateLayoutController,
        childBuilder: _renderContent,
        dataCallback: (data) {
          print(data);
          _data = ShuntingModel.fromJson(data);
          print(_data);
        },
      ).padding(EdgeInsets.only(
          left: MediaQuery.of(context).padding.left,
          right: MediaQuery.of(context).padding.right)),
    );
  }

  Widget _renderContent(dynamic data) {
    return ListView(
      children: [
        _renderTop(),
        10.hspace,
        Container(
          width: ScreenUtil.getInstance().screenWidth,
          color: UIColor.color_content,
          child: UText.get("变更终点", UIColor.color_primary, 14.rpx).padding(
              EdgeInsets.only(left: 16.rpx, top: 10.rpx, bottom: 10.rpx)),
        ),
        _renderBottom()
      ],
    );
  }

  Widget _renderTop() {
    return Container(
      decoration: const BoxDecoration(
        color: UIColor.color_content,
      ),
      child: Column(
        children: [
          16.hspace,
          DetailFlexStartLayout('车辆编号', _data?.car_no ?? "",
              valueColor: UIColor.color_primary, valueFont: FontWeight.bold),
          12.hspace,
          DetailFlexStartLayout('操作人', _data?.operator_name ?? ""),
          12.hspace,
          DetailFlexStartLayout('起始位置', _data?.start_station_name ?? ""),
          12.hspace,
          DetailFlexStartLayout('终点位置', _data?.end_station_name ?? ""),
          12.hspace,
          DetailFlexStartLayout('开始时间', _data?.start_time ?? ""),
          12.hspace,
          DetailFlexStartLayout('结束时间', _data?.end_time ?? ""),
          16.hspace,
        ],
      ),
    );
  }

  Widget _renderBottom() {
    List list = _data?.list ?? [];
    return Container(
      decoration: const BoxDecoration(
        color: UIColor.color_content,
      ),
      child: Wrap(
        children:
            list.asMap().keys.map((index) => _renderOne(list, index)).toList(),
      ),
    );
  }

  Widget _renderOne(List list, int index) {
    return Column(
      //direction: Axis.vertical,
      children: [
        0.5.hLine16,
        12.hspace,
        DetailFlexStartLayout('操作人', list[index]["operator_name"]),
        12.hspace,
        DetailFlexStartLayout('变更位置', list[index]["end_station_name"]),
        12.hspace,
        DetailFlexStartLayout('操作时间', list[index]["operation_time"]),
        16.hspace,
      ],
    );
  }
}
